/**
 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
#if 1

#include "HAL/HAL.h"
#include "display.h"
#include "App/App.h"


void setup()
{
    HAL::Init();
    
    App_PortInit();
    App_Init();
    // INIT_DONE();
}

void start()
{
    App_PortStart();    
}


void loop()
{
    HAL::Update();
    
}

int main() 
{
    setup();

    start();

    while(1)
    {
        loop();
    }


}
#else
#include <stdio.h>

#include <stdbool.h>
#include "pico/stdlib.h"
//#include "LCD_Driver.h"
#include "lvgl.h"
#include "lv_port_disp.h"
#include "lv_port_indev.h"
#include "lv_demos.h"

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "time.h"

#include "LCD_Driver.h"
#include "Key_Driver.h"

#if 1 
QueueHandle_t xToFlushQueue = NULL;

void APP_LCD_display_window(int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint16_t* image)
{

    LCD_display_window( x1,  y1,  x2,  y2,  image);
}

uint32_t APP_Keypad_read()
{
    return Keypad_read();
}

void vApplicationTickHook()
{
	lv_tick_inc(1);
}


static portTASK_FUNCTION(lv_timer_task_handler, pvParameters)
{
	TickType_t xLastWakeTime;
	
	xLastWakeTime = xTaskGetTickCount();  
	
	for(;;) {		
		vTaskDelayUntil( &xLastWakeTime,pdMS_TO_TICKS( 5 ) );
		lv_timer_handler();
	}
	vTaskDelete(NULL);
}

static portTASK_FUNCTION(led_task_handler, pvParameters)
{
	TickType_t xLastWakeTime;
	
	xLastWakeTime = xTaskGetTickCount();  
	
	for(;;) {		
		vTaskDelay(pdMS_TO_TICKS( 500 ) );
		gpio_put(PICO_DEFAULT_LED_PIN, 1);
        vTaskDelay(pdMS_TO_TICKS( 500 ) );
		gpio_put(PICO_DEFAULT_LED_PIN, 0);
	}
	vTaskDelete(NULL);
}

int main(void)
{
    //stdio_uart_init_full(uart0, 115200, 16, 17);

    //printf("\n\n\nPICO DM QD3503728 LVGL Porting\n");

    //xToFlushQueue = xQueueCreate(2, sizeof(struct video_frame));
    gpio_init(PICO_DEFAULT_LED_PIN);
    gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);

    stdio_init_all();

    LCD_init();
    Keypad_creat();

    lv_init();
    lv_port_disp_init();
    lv_port_indev_init();

    //printf("Starting demo\n");
    // lv_example_btn_1();
    // lv_demo_widgets();
    // lv_demo_stress();
    // lv_demo_music();

    /* measure weighted fps and opa speed */
    //lv_demo_benchmark();
    lv_demo_keypad_encoder();
    /* This is a factory test app */
    // factory_test();
 
    
    TaskHandle_t lvgl_task_handle;
    xTaskCreate(lv_timer_task_handler, "lvgl_task", 2048, NULL, (tskIDLE_PRIORITY + 3), &lvgl_task_handle);
    vTaskCoreAffinitySet(lvgl_task_handle, (1 << 0));

    TaskHandle_t led_task_handle;
    xTaskCreate(led_task_handler, "led_task", 1048, NULL, (tskIDLE_PRIORITY + 3), &led_task_handle);
    vTaskCoreAffinitySet(led_task_handle, (1 << 1));
/*
    TaskHandle_t video_flush_handler;
    xTaskCreate(video_flush_task, "video_flush", 256, NULL, (tskIDLE_PRIORITY + 2), &video_flush_handler);
    vTaskCoreAffinitySet(video_flush_handler, (1 << 1));
*/
    vTaskStartScheduler();

    for(;;);

    return 0;
}

#else
void vApplicationTickHook()
{
	//lv_tick_inc(1);
}

bool repeating_timer_callback(struct repeating_timer *t) {
     lv_tick_inc(5);
    return true;
}

int main() {
    const uint LED_PIN = PICO_DEFAULT_LED_PIN;
    int i;
    stdio_init_all();
    //printf("Hello Timer!\n");

    struct repeating_timer timer;
    add_repeating_timer_ms(5, repeating_timer_callback, NULL, &timer);


   gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);
    for(i=0;i<3;i++) 
    {
        gpio_put(LED_PIN, 1);
        sleep_ms(50);
        gpio_put(LED_PIN, 0);
        sleep_ms(50);
    }
    

    lv_init();
    lv_port_disp_init();
    lv_port_indev_init();

    //lv_user_gui_init();
    //lv_tick_set_cb();
    
    //lv_demos_create(NULL,-1);
    //lv_demos_create("keypad_encoder",1);
    //lv_demo_benchmark();   
   //lv_demo_widgets();
    //lv_example_get_started_2();
    lv_demo_keypad_encoder();
    //lv_demo_music();
    //lv_demo_widgets();

  
    while(1){
            
        sleep_ms(5);
        lv_task_handler();
        
        //lv_tick_inc(5);
    
    }

    //lv_demo_keypad_encoder();
    /*while(1)
    {
        i=0;
        while(i<2000){
              
            sleep_ms(5);
            lv_task_handler();
            i++;
            //lv_tick_inc(5);
        
        }
        lv_demo_benchmark(); 
    }*/
}
#endif
#endif